package com.kgc.sbt.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.BuyVip;
import com.kgc.sbt.api.ReceiveCouponsApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.nio.charset.StandardCharsets;

/**
 * Created on 2021/8/20.
 * <p>
 * Author : Lcywings
 * <p>
 * Description :
 */
@Slf4j
public class RushBListener extends KeyExpirationEventMessageListener {

    @Reference
    private ReceiveCouponsApi receiveCouponsApi;

    @Reference
    private BuyVip buyVip;

    public RushBListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("redis key 过期：pattern={},channel={},key={}", new String(pattern), channel, key);
        String[] keys = key.split(":");
        if (keys[0].equals("appointment")) {
            log.info("== 向用户:{},手机：{}，发送短信：发送的内容是：您的货：{}，即将开抢", keys[1], keys[2], keys[3]);
        }
        if (keys[0].equals("coupons")) {
            receiveCouponsApi.deleteBycouponId(Integer.valueOf(keys[1]));
        }
        if (keys[0].equals("userVip")) {
            buyVip.queryDaysBuyUserId(Integer.valueOf(keys[1]));
        }
        if (keys[0].equals("addUserVip")) {
            log.info("userId:{}", keys[1]);
            buyVip.queryDaysBuyUserId(Integer.valueOf(keys[1]));
        }
    }
}